package com.wind.util;

import java.util.ArrayList;
import java.util.List;

public class DimToExpUtil {
    //获取到无序的所有组合
    private static void combineIncrease(List<String> arr, int start, int[] result, int count, int num, List<String> tmpList) {
        for (int i = start; i < arr.size() + 1 - count; i++) {
            result[count - 1] = i;
            if (count - 1 == 0) {
                String tmp = "";
                for (int j = num - 1; j >= 0; j--) {
                    tmp += arr.get(result[j]);
                }
                tmpList.add(tmp);
            } else {
                combineIncrease(arr, i + 1, result, count - 1, num, tmpList);
            }
        }
    }

    //获取固定个数的有序组合
    private static void rotate(String[] result, List al, int index, List<String> ls) {
        index = result.length - index;
        if (index == result.length - 1) {
            result[index] = (String) al.get(0);
            organizeNum(result, ls);
        }
        for (int s1 = 0; s1 < al.size(); s1++) {
            result[index] = (String) al.get(s1);
            ArrayList al1 = new ArrayList(al);
            al1.remove(result[index]);
            rotate(result, al1, al1.size(), ls);
        }
    }

    private static void organizeNum(String[] result, List<String> ls) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < result.length; i++) {
            if (i == result.length - 1) {
                sb.append(result[i]);
            } else {
                sb.append(result[i] + ",");
            }
        }
        String pStr = sb.toString();
        ls.add(pStr);
    }

    private static List<String> stringToList(String s) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            list.add("" + s.charAt(i));
        }
        return list;
    }

    public static List<String> orderGroup(List<String> source, int num) {
        List<String> tmpList = new ArrayList<>();
        List<String> result = new ArrayList<>();
        combineIncrease(source, 0, new int[num], num, num, tmpList);
        for (String tmp : tmpList) {
            rotate(new String[num], stringToList(tmp), num, result);
        }
        return result;
    }

    public static void main(String[] args) {
        List<String> data = new ArrayList<>();
        data.add("1");
        data.add("2");
        data.add("3");
        data.add("4");
        data.add("5");
        List<String> list = DimToExpUtil.orderGroup(data, 3);
        for (String s1 : list) {
            System.out.println(s1);
        }
    }
}
